<template>
  <div class="film">
    <div class="draw" v-if="loadState == true">
      <div class="draw-list" v-for="(de, di) in movieArr" :key="di">
        <img :src="`${de.img}`" alt="" />
        <div class="txt">
          <!-- 上 -->
          <div class="txt-top">
            <div class="txt-top-1">
              <p class="top-p1">{{ de.nm }}</p>
              <p class="top-p2">{{ de.sc }}</p>
            </div>
            <span class="txt-top-2">{{ de.enm }}</span>
          </div>
          <!-- 中 -->
          <div class="txt-center">
            <p class="center-p1">
              <span class="center-s1">
                {{ /^[\u4e00-\u9fa5]{0,}/.exec(de.cat)[0] }}</span
              >
              {{ de.cat.split(",").splice(1).join() }}
              <!-- {{ /^[\u4e00-\u9fa5]{0,}/.exec(de.cat)[0] }} -->
            </p>
          </div>
          <!-- 下 -->
          <div class="txt-bottom">
            <p>{{ de.star }}</p>
            <p>{{ de.pubDesc }}</p>
            <!-- <p>{{de.hasOwnProperty('showStateButton')}}</p> -->
          </div>
        </div>
      </div>
      <!-- <p class="draw-p">
        查看全部{{ moviesData.total }}部相关影视综作品
        <i class="iconfont icon-xiangyou1"></i>
      </p> -->
    </div>
    <div class="loading" v-if="loadState == false">
      <p class="text-flowLight">正在加载...</p>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";

export default {
  name: "cartFilm",
  props: ["msg"],
  data() {
    return {
      movieArr: [],
      loadState: -1, //loading状态
      peoffset: 20, //初始化影片数据
      isAchiveBottom: false, //滚动条是否到底部标志
    };
  },
  methods: {
    ...mapMutations(["changeSearchArray"]),
    getData(k) {
      this.loadState = false;
      let pro = axios.get("search/movies", {
        params: {
          keyword: this.msg,
          cityId: 1,
          offset: k,
          //  offset: 20,
        },
      });
      pro
        .then((res) => {
          setTimeout(() => {
            this.loadState = true;
            // this.movieArr = res.movies;
            this.disposeData(res);
          }, 1000);

          console.log("请求成功----->", res);
        })
        .catch((err) => {
          console.log("请求失败----->", err);
        });
    },
    //  触底加载方法  需要优化
    bottomOut() {
      window.onscroll = () => {
        //变量scrollTop是滚动条滚动时，距离顶部的距离
        let scrollTop =
          document.documentElement.scrollTop || document.body.scrollTop;
        //变量windowHeight是可视区的高度
        let windowHeight =
          document.documentElement.clientHeight || document.body.clientHeight;
        //变量scrollHeight是滚动条的总高度
        let scrollHeight =
          document.documentElement.scrollHeight || document.body.scrollHeight;
        //   console.log(scrollTop, windowHeight, scrollHeight);
        //  -------
        if (
          scrollTop + windowHeight >= scrollHeight - 50 &&
          this.isAchiveBottom == false
        ) {
          console.log("达到最小值--搜索电影触底----");
          this.isAchiveBottom = true;
          setTimeout(() => {
            this.changeData();
            this.isAchiveBottom = false;
          }, 2000);
        }
      };
    },
    //  处理 触底 数据
    changeData() {
      this.peoffset += 20;
      this.peoffset = this.peoffset;
      console.log("处理 触底 数据-----", this.peoffset);
      if (this.peoffset < 100) {
        this.getData(this.peoffset);
      } else {
        console.log("没有更多了。。。");
      }
    },
    disposeData(t) {
      // 合并数组
      if (this.$store.state.searchArray.length == 0) {
        //   赋值
        // this.movieArr = [];
        this.movieArr = t.movies;
      } else {
        console.log("每次更新数据----->", t.movies);
        // this.movieArr = [];
        this.movieArr = this.$store.state.searchArray.concat(t.movies);
        //  console.log(this.$store.state.homepageArray.length == 0 );
      }
      //  存值
      this.changeSearchArray(this.movieArr);
    },
  },
  mounted() {
    this.getData(20);
    // console.log(this.msg);
    this.bottomOut();
    //   var pattern = /([\u4e00-\u9fa5]*)*/,
    // let str = "电视剧,剧情,喜剧,爱情";
    // console.log(str.split(",").splice(1));

    //  console.log(pattern.exec(str));
  },
};
</script>

<style lang="scss" scoped>
.film {
  width: 100%;
  height: 100%;
  position: relative;
}
// 渲染数据
.draw {
  .draw-list {
    background: rgb(230, 230, 230);
    padding: 7px 0;
    border-radius: 7px;
    width: 352px;
    margin: 14px auto;
    display: flex;
    justify-content: space-between;
    img {
      width: 80px;
      height: 110px;
      border-radius: 6px;
      object-fit: cover;
    }
    // 文本区
    .txt {
      width: 255px;
      // 上
      .txt-top {
        width: 100%;
        height: 40px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-content: flex-start;
        .txt-top-1 {
          display: flex;
          justify-content: space-between;
          .top-p1 {
            font-size: 16px;
            color: #000;
            font-weight: bold;
          }
          .top-p2 {
            font-size: 14px;
            color: #ffa82e;
          }
        }
        .txt-top-2 {
          display: inline-block;
          font-size: 12px;
          flex-wrap: 300;
        }
      }
      // 中
      .txt-center {
        margin: 3px 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .center-p1 {
          font-size: 14px;
          .center-s1 {
            font-size: 10px;
            box-sizing: border-box;
            color: #67a6ff !important;
            border-radius: 3px;
            border: 1px solid #85a9da !important;
          }
        }
        .center-p2 {
          padding: 6px 14px;
          border-radius: 18px;
          font-size: 14px;
          background: #faaf00;
          color: rgb(245, 245, 245);
        }
      }

      // 下
      .txt-bottom {
        p {
          font-size: 10px;
          padding: 3px 0;
        }
      }
    }
  }
  .draw-list:first-child {
    //  margin: 0 auto 14px;
  }
  .draw-p {
    background: rgb(237, 255, 203);
    font-size: 16px;
    color: #f34c40;
    // padding: 1px 0;
    width: 100%;
    height: 50px;
    text-align: center;
    line-height: 50px;
  }
}
.loading {
  width: 100px;
  height: 30px;
  border-radius: 7px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  background: rgba(0, 0, 0, 0.3);
}
.text-flowLight {
  position: relative;
  width: 100px;
  text-align: center;
  line-height: 30px;
  background-image: -webkit-linear-gradient(
    left,
    #b1c0ff,
    #b3ffb3,
    #ffbaba 100%
  );
  -webkit-text-fill-color: transparent;
  -webkit-background-clip: text;
  -webkit-background-size: 200% 100%;
  -webkit-animation: masked-animation 2s forwards linear infinite;
  font-weight: 700;
  white-space: nowrap;
  font-size: 16px;
}
@keyframes masked-animation {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -200% 0;
  }
}
</style>